package com.ztjc.day04_newsclient.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.ztjc.day04_newsclient.R;
import com.ztjc.day04_newsclient.domain.News;

import java.util.List;

/**
 * Created by pro on 15/10/9.
 */
public class NewsListAdapter extends BaseAdapter {

    private Context context;
    private List<News> newses;

    public NewsListAdapter(Context context, List<News> newses) {
        this.context = context;
        this.newses = newses;
    }

    @Override
    public int getCount() {
        return newses.size();
    }

    @Override
    public Object getItem(int position) {
        return newses.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        News news = newses.get(position);
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.news_item, null);
            viewHolder = new ViewHolder();
            viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.detail = (TextView) convertView.findViewById(R.id.detail);
            viewHolder.commont = (TextView) convertView.findViewById(R.id.commont);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.title.setText(news.getTitle());
        viewHolder.detail.setText(news.getDetail());
        viewHolder.commont.setText("评论" + news.getComment());
        Picasso.with(context).load(news.getImage()).into(viewHolder.image);
        return convertView;
    }

    class ViewHolder {
        ImageView image;
        TextView title;
        TextView detail;
        TextView commont;
    }
}
